package com.yueka.controller;


import com.yueka.pojo.system_user.LoginInfo;
import com.yueka.pojo.Result;
import com.yueka.pojo.system_user.Password;
import com.yueka.pojo.system_user.SystemUser;
import com.yueka.service.SystemUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/system_user")
@RestController
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @PostMapping("/login")
    public Result login(@RequestBody SystemUser systemUser) {
        try {
            LoginInfo loginInfo = systemUserService.login(systemUser);
            if (loginInfo == null) {
                return Result.error("用户名或密码错误！");
            }
            return Result.success(loginInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody SystemUser systemUser) {
        try {
            return Result.success(systemUserService.register(systemUser));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/getList")
    public Result getList(String keyword, String status, Integer page, Integer pageSize) {
        try {
            return Result.success(systemUserService.getList(keyword, status, page, pageSize));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody SystemUser systemUser) {
        try {
            return Result.success(systemUserService.update(systemUser));
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/delete")
    public Result delete(Integer[] ids) {
        try {
            return Result.success(systemUserService.delete(ids));
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/updatePersonal")
    public Result updatePersonal(@RequestBody SystemUser systemUser, HttpServletRequest request) {
        try {
            systemUserService.updatePersonal(systemUser, request);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/resetPassword")
    public Result resetPassword(Integer id) {
        try {
            systemUserService.resetPassword(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Password password, HttpServletRequest request) {
        try {
            systemUserService.updatePassword(password.getOldPassword(), password.getNewPassword(), request);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
